<?php

namespace App\Http\Controllers\Game\Data;

use App\Core\JWT;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use App\Http\Controllers\Controller;
use App\Http\Resources\Common\DataResource;
use App\Http\Resources\Common\DatasResource;
use App\Services\Game\Data\Settlement\InfoService as SettlementInfoService;
use App\Models\Game\Config\System as Config;
use App\Models\Game\Settlement\Detail;

class SettlementController extends Controller
{

    /**
     * 导出数据
     */
    public function export(Request $request)
    {
        SettlementInfoService::create()->getList($request, true);
    }

    /**
     * 获取数据列表
     */
    public function getList(Request $request)
    {
        list ($collection, $total, $stats) = SettlementInfoService::create()->getList($request);

        return DatasResource::create($collection)->setPaginateTotal($total)->setAttribute('stats', $stats);
    }

    /**
     * 获取详情链接
     */
    public function getDetail(Request $request)
    {
        $data = $request->validate([
            'sn' => 'required|string',
        ]);

        $token = JWT::instance()->getToken($data);

        $domain = Config::getDomain()['agency'];

        return new DataResource([
            'url' => "{$domain}/game/detail/{$token}?lang=" . App::getLocale(),
        ]);
    }

    /**
     * 获取详情数据
     */
    public function getDetailData(Request $request)
    {
        if (!$request->user()->isSuperAdmin()) {
            // 提示没有权限
            abort(403);
        }
        $data = $request->validate([
            'round_id' => 'required|string',
        ]);

        if (is_null($detail = Detail::getDetail($data['round_id']))) {
            return abort(404);
        }

        //  计算平台场控结束时间    场控开始时间 + （持续时间 * 1000）
        if (isset($detail['rtp_control']['rtpPeriodRevise'])){
            $detail['rtp_control']['rtpPeriodRevise']['endTime'] = $detail['rtp_control']['rtpPeriodRevise']['startTime'] + ($detail['rtp_control']['rtpPeriodRevise']['durationTime'] * 1000);
        }
        return new DataResource($detail);
    }
}
